//
//  GalleryViewController.m
//  
//
//  Created by fabian on 05.03.11.
//  Copyright 2011 roboboogie.us. All rights reserved.
//

#import "GalleryViewController.h"
#import "ZoomingScrollView.h"


#define THUMB_HEIGHT 100
#define THUMB_V_PADDING 10
#define THUMB_H_PADDING 10
#define PAGING_HEIGHT 640

@implementation GalleryViewController
@synthesize pagingScrollView;
@synthesize imageNames;
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	[[self view] setBackgroundColor:[UIColor blackColor]];
	
	imageNames = [NSArray arrayWithObjects:@"1911_dieMetropoleWartet_01.jpg",@"1911_dieMetropoleWartet_02.jpg",@"1911_dieMetropoleWartet_08.jpg",nil];
	
	///////// ZURÜCKBUTTON mit homebuttonView Delegate
	
	CGRect frame = CGRectMake(10, 643, 100, 20);
	HomeButtonView *button = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"zurück" andButtonFunction:@"backToArticle" ];
	button.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

	///WICHTIG sich selbst als delegate melden 
	[button setDelegate:self];
	[[self view] addSubview:button];
	[button release];
	
	/////////
	
	
	CGRect pagingScrollViewFrame = CGRectMake(0,0,1024,PAGING_HEIGHT);
	pagingScrollViewFrame.size.width+=20;
	
	pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
	
	pagingScrollView.pagingEnabled = YES;
	
	pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [imageNames count],
											  pagingScrollViewFrame.size.height);
	
	
	[[self view] addSubview:pagingScrollView];
	
	for (int i = 0; i<[imageNames count]; i++) {
		ZoomingScrollView *zoomingScrollView = [[ZoomingScrollView alloc] initWithFrame:CGRectMake(pagingScrollViewFrame.size.width*i,0, 1024, PAGING_HEIGHT) andImageName:[imageNames objectAtIndex:i]];
		if(zoomingScrollView.imageView.image.size.height>PAGING_HEIGHT) {
			float scale = (PAGING_HEIGHT/(zoomingScrollView.imageView.image.size.height/100))/100;
			[zoomingScrollView setZoomScale:scale animated:NO];
		}
		
		[pagingScrollView addSubview:zoomingScrollView] ;
		[zoomingScrollView release];
	}
	
	int scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;
	UIScrollView *thumbScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 641, [[self view] bounds].size.height, scrollViewHeight)];
	[thumbScroll setCanCancelContentTouches:NO];
	[thumbScroll setClipsToBounds:NO];
	
	float xPosition = THUMB_H_PADDING;
	for (int i = 0; i<[imageNames count]; i++) {
		UIImage *thumbImage = [UIImage imageNamed:[NSString stringWithFormat:@"_%@", [imageNames objectAtIndex:i]]];
		if (thumbImage) {
			ThumbImageView *thumb = [[ThumbImageView alloc] initWithImage:thumbImage];
			[thumb setDelegate:self];
			[thumb setSomeId:i];
			CGRect frame = [thumb frame];
			frame.origin.y = THUMB_V_PADDING;
			frame.origin.x = xPosition;
			[thumb setFrame:frame];
			[thumb setHome:frame];
			[thumbScroll addSubview:thumb];
			[thumb release];
			xPosition += (frame.size.width + THUMB_H_PADDING);
		}
	}
	
	[thumbScroll setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
	if ([thumbScroll contentSize].width<[[self view] bounds].size.height) {
		CGRect aFrame = thumbScroll.frame;
		aFrame.origin.x = ([[self view] bounds].size.height - [thumbScroll contentSize].width)/2;
		thumbScroll.frame = aFrame;
	}
	
	[[self view] addSubview:thumbScroll];	
}

-(void) changePage:(int)pId {
	CGRect frame = pagingScrollView.frame;
	frame.origin.x = frame.size.width * (pId);
	frame.origin.y = 0;
	[pagingScrollView scrollRectToVisible:frame animated:YES];
}


#pragma mark HomeButtonViewDelegate methods

-(void) homeButtonViewWasTapped:(HomeButtonView *)hbv {
	[self.navigationController popViewControllerAnimated:NO];
}


#pragma mark ThumbImageViewDelegate methods 

-(void) thumbImageViewWasTapped:(ThumbImageView *)tiv {
	[self changePage:tiv.someId];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}


- (void)dealloc {
	[pagingScrollView release];
	[imageNames release];
    [super dealloc];
}


@end
